package cn.shujuhai.qtadmin.platform.kit;

import javax.servlet.http.HttpServletRequest;

import org.jetbrains.annotations.NotNull;

import cn.hutool.core.text.CharSequenceUtil;

/**
 * 公共工具类
 *
 * @author dch798
 * @date 2021/08/26 22:23
 **/
public class CommonKit {

    public static final String XML_HTTP_REQUEST = "XMLHttpRequest";

    private CommonKit() {}

    /**
     * @param request
     *            参数
     * @return 是否json请求
     */
    public static @NotNull Boolean isJson(@NotNull HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        String requestAccept = request.getHeader("accept");
        if (XML_HTTP_REQUEST.equalsIgnoreCase(requestedWith)) {
            return true;
        }
        if (CharSequenceUtil.isNotEmpty(requestAccept)) {
            return CharSequenceUtil.contains(requestAccept, "application/json")
                || CharSequenceUtil.contains(requestAccept, "text/javascript")
                || CharSequenceUtil.contains(requestAccept, "text/json");
        }
        return false;
    }
}
